<template>
	<view>

	</view>
</template>

<script>
	var app = getApp();
	export default {
		data() {
			return {
				paymentApi: '',
				orderUrl: '',
				openid: '',
				source: '',
				orderId:''
			};
		},
		onLoad: function(e) {
			console.log(e,'地址')
			this.orderUrl = e.orderUrl
			this.orderId = e.orderId
			this.init()
		},
		methods: {
			init() {
				var that = this;
				app.globalData.util.request({
					url: 'Webview/orders',
					
					cachetime: '30',
					success: function(res) {
						// console.log(res,'rrrrrrrrrrrrrrrrrre')
						that.openid = res.data.data.openid
						that.paymentApi = res.data.data.paymentApi
						that.getpayInfo()
					}
				}, true);
			},
			// 使用订单号获取支付信息
			getpayInfo() {
				let data = {
					openid: this.openid,
					order_no: this.orderId
				}
				console.log('111')
				var that = this;

			 uni.request({
					url: this.paymentApi, //仅为示例，并非真实的接口地址
					data: data,
					success: function(res) {
						console.log(res, '-----------')
						if (res.data.code == 1) {
							console.log('获取到wxpay',res.data.data.wxpay)
							that.payMoney(res.data.data.wxpay)
				
							console.log('获取到wxpay',res.data.data.wxpay)
						} else {
							console.log('333')
							wx.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					}
				})
			},
			// 付款
			payMoney(data) {
				console.log('这里是付款',data)
				var that = this;
				wx.requestPayment({
					// ...data,
					timeStamp: data.timeStamp,
					nonceStr: data.nonceStr,
					package: data.package,
					signType: data.signType,
					paySign: data.paySign,
					success: () => {
						//支付成功
					// uni.navigateTo({
					// 	url: "/pages/openwebview/openwebview?url=" + this.orderUrl
					// })
					that.addorders();
					console.log(this.orderUrl,'支付成功返回编码');
					},
					fail: (res) => {
						//支付失败
						console.log(res, '支付失败----')
						wx.showToast({
							title: '支付失败',
							icon: 'none'
			  	})
				// uni.reLaunch({
				// 	url:'/gc_school/pages/home/index'
				// })
					uni.navigateBack({
						delta:1,//返回层数，2则上上页
					})
					}
				})
			},
			// 添加支付订单
			addorders(){
				var that = this;
				app.globalData.util.request({
					url: 'Webview/jtkcallback',
					data:{
						order_sn:that.orderId,
						s_id:uni.getStorageSync('schoolId') 
					},
					cachetime: '30',
					success: function(res) {
						uni.navigateTo({
							url: "/pages/openwebview/openwebview?url=" + that.orderUrl
						})
					}
				}, true);
			}
		}
	};
</script>

<style>
</style>
